package cool.qingqing.streamapi;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sun.rmi.runtime.Log;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 高山青
 * @Date 2022/1/3  -   17:25
 */
public class StreamApiDemo06 {
	long start;

	@Before
	public void start(){
		start=System.currentTimeMillis();
	}

	/**
	 * 流转成集合
	 */
	@Test
	public void toList(){
		//转成list集合
		List<String> list=Stream.of("1","4","5","34").collect(Collectors.toList());
		list.forEach(s -> System.out.print(s+"  "));
		System.out.println();
		//转成set集合
		Set<String> set=Stream.of("1","4","5","34").collect(Collectors.toSet());
		set.forEach(s -> System.out.print(s+"  "));
		System.out.println();
		//转成ArrayList集合
		List<String> arrayList=Stream.of("1","4","5","34").collect(Collectors.toCollection(ArrayList::new));
		arrayList.forEach(s -> System.out.print(s+"  "));
		System.out.println();
		//集合转成数组
		Object[] s = list.stream().toArray();
		Arrays.copyOfRange(s,0,s.length,String[].class);




	}

	/**
	 * 统计操作
	 *
	 */
	@Test
	public void statistics(){
		//求和操作
		long sum = Stream.of(new Person("王小二", 18),
				new Person("小刚", 18),
				new Person("小红", 18),
				new Person("小李", 18),
				new Person("小高", 18)).collect(Collectors.summarizingInt(Person::getAge)).getSum();
		System.out.println("年龄的和是："+sum);
		//求平均值操作
		Double avg = Stream.of(
				new Person("王小二", 18),
				new Person("小刚", 21),
				new Person("小红", 13),
				new Person("小李", 15),
				new Person("小高", 26)).collect(Collectors.averagingDouble(Person::getAge));
		System.out.println("年龄的平均值是："+avg);
		//求数量操作
		long count = Stream.of(new Person("王小二", 18),
				new Person("小刚", 18),
				new Person("小红", 18),
				new Person("小李", 18),
				new Person("小高", 18)).collect(Collectors.counting());
		System.out.println("人数："+count);
		//求最大值
		Person max = Stream.of(
				new Person("王小二", 18),
				new Person("小刚", 21),
				new Person("小红", 13),
				new Person("小李", 15),
				new Person("小高", 26)).collect(Collectors.maxBy((p1, p2) -> p1.getAge() - p2.getAge())).get();
		System.out.println("年龄最大的人是："+max);
		//求最小值
		Person min = Stream.of(
				new Person("王小二", 18),
				new Person("小刚", 21),
				new Person("小红", 13),
				new Person("小李", 15),
				new Person("小高", 26)).collect(Collectors.minBy((p1, p2) -> p1.getAge() - p2.getAge())).get();
		System.out.println("年龄最小的人是："+min);
		//分组
		Map<String, List<Person>> groupby = Stream.of(
				new Person("王小二", 18),
				new Person("小刚", 21),
				new Person("小红", 13),
				new Person("小李", 15),
				new Person("小高", 26)).
				collect(
						Collectors.groupingBy((p1) -> p1.getAge() > 18 ? "成年" : "未成年")
				);

		groupby.forEach((k,v)->System.out.println("k="+k+"  "+"v="+v));

		Map<String, Map<String, List<Person>>> groupbytwo = Stream.of(
				new Person("王小二", 18),
				new Person("小刚", 21),
				new Person("小红", 13),
				new Person("小李", 15),
				new Person("小高", 26)).
				collect(Collectors.groupingBy((p1) -> p1.getName().length() == 3 ? "三位名" : "其他名",
						Collectors.groupingBy(p1 -> p1.getAge() >= 18 ? "成年" : "未成年"))
				);

		groupbytwo.forEach((k,v)->{
			System.out.println(k);
			v.forEach((i,j)->{
				System.out.println("		"+i+"  "+j);
			});
		});

		Map<String, Map<String, IntSummaryStatistics>> groupbythree = Stream.of(
				new Person("王小二", 18),
				new Person("小刚", 21),
				new Person("小红", 13),
				new Person("小李", 15),
				new Person("小高", 26)).
				collect(
						Collectors.groupingBy(p1 -> p1.getAge() >= 18 ? "成年" : "未成年",
								Collectors.groupingBy(p1 -> p1.getAge() >= 18 ? "成年" : "未成年",
										Collectors.summarizingInt(Person::getAge)))
				);

		groupbythree.forEach((k,v)->{
			System.out.println(k);
			v.forEach((i,j)->{
				System.out.println("		"+i+"  "+j.getSum());
			});
		});

		//分区操作
		Map<Boolean, List<Person>> partition = Stream.of(
				new Person("王小二", 18),
				new Person("小刚", 21),
				new Person("小红", 13),
				new Person("小李", 15),
				new Person("小高", 26)).
				collect(
						Collectors.partitioningBy(p1 -> p1.getAge() >= 18 ? true : false)
				);
		partition.forEach((k,v)->{
			System.out.println(k);
			v.forEach(i->{
				System.out.println("      "+i);
			});

		});
		//拼接字符串
		String names=Stream.of(
				new Person("王小二", 18),
				new Person("小刚", 21),
				new Person("小红", 13),
				new Person("小李", 15),
				new Person("小高", 26)).map(Person::getName).collect(Collectors.joining(","));
		System.out.println(names);
	}

	@After
	public  void end(){
		System.out.println();
		StreamApiDemo06 s=new StreamApiDemo06();
		long end=System.currentTimeMillis();
		System.out.println("用时"+(end-start));
	}
}
